package com.probiz.estore.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.system.Carrier;
import com.probiz.estore.common.model.system.ShippingMethod;
import com.probiz.estore.common.model.system.ShippingRate;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.eai.shippingchoice.generated.ShippingChoice;
import com.probiz.estore.system.dao.ShippingMethodDao;
import com.probiz.estore.system.service.CarrierManager;
import com.probiz.estore.system.service.ShippingRateManager;


/**
 * ShippingMethod Business Delegate (Proxy) implementation to handle communication between web and
 * persistence layer. Implementation of ShippingMethodManager interface.
 * Developer introduced interfaces should be declared here. Won't get overwritten.
 */
public class ShippingMethodManagerImpl extends GenericManagerImpl<ShippingMethod> implements com.probiz.estore.system.service.ShippingMethodManager {
   private CarrierManager carrierManager = null;
   private ShippingRateManager shippingRateManager ;
   private ShippingMethodDao shippingMethodDao = null;
   
    public void setShippingMethodDao(ShippingMethodDao shippingMethodDao) {
    	this.shippingMethodDao = shippingMethodDao;
	}
    

	public void setShippingRateManager(ShippingRateManager shippingRateManager) {
		this.shippingRateManager = shippingRateManager;
	}


	public void setCarrierManager(CarrierManager carrierManager) {
		this.carrierManager = carrierManager;
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.system.service.ShippingMethodManager#listAllActive()
	 */
	public List<ShippingMethod> listAllActive(){
		return shippingMethodDao.listAllActive();
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.system.service.ShippingMethodManager#findUnDeletedShippingMethods()
	 */
	public List<ShippingMethod> findUnDeletedShippingMethods(){
		return shippingMethodDao.findByHql("from ShippingMethod s where s.deleted != ? order by s.shippingMethodId asc", Constants.FLAG_TRUE);
	}

	public void doImportShippingMethodsForEai(List<ShippingChoice> shippingChoiceList) {
		for(ShippingChoice shippingChoice : shippingChoiceList){
			doImportShippingChoiceForEai(shippingChoice);
		}
		
	}
	private void doImportShippingChoiceForEai(ShippingChoice shippingChoice){
		String code=shippingChoice.getCode();
		ShippingMethod shippingMethod=shippingMethodDao.findShippingMethodByShippingMethodCode(code);
		Carrier carrier=this.carrierManager.getCarrierForEai();
		if(shippingMethod==null){//new			
			shippingMethod=new ShippingMethod();
			shippingMethod.setShippingMethodCode(code);
			shippingMethod.setDeleted(new Short("0"));
			shippingMethod.setIsCod(new Short("0"));
			shippingMethod.setCarrier(carrier);
			shippingMethod.setStatus(Constants.STATUS_ACTIVE);
			shippingMethod.setShippingMethodName(shippingChoice.getName());
		}else{			
			shippingMethod.setShippingMethodName(shippingChoice.getName());
		}
		this.dao.save(shippingMethod);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.system.service.ShippingMethodManager#findByRegionIdByCod(java.lang.Boolean, java.lang.Integer)
	 */
	public List<ShippingMethod> findByRegionIdByCod(Boolean isCod,Integer regionId){
		List<ShippingMethod> result = new ArrayList<ShippingMethod>();
		List<ShippingRate> shippingRateList = shippingRateManager.getDifferentShipMethodRatesByRegionId(isCod,regionId);
		for(ShippingRate shippingRate : shippingRateList){
			result.add(shippingRate.getShippingMethod());
		}
		return result;
	}

	@Override
	protected void initManager() {
	
		dao = shippingMethodDao;
	}

	@Override
	protected void preDelete(ShippingMethod entity) {
		
	}

	@Override
	protected void preSave(ShippingMethod entity) {
		
	}

	public List<ShippingMethod> getActiveServices() {
		return null;
	}

}
